<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
$return['banner']=$basic['image'];
if($_RQ['op']=='list'){
	$categoryid=$_RQ['categoryid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$return['params']=array();
	if($categoryid){
		$params['categoryid']=$categoryid;
		$return['params']['categoryid']=$categoryid;
	}
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$serve_list=pdo_getslice('jy_gohome_serve',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	if(empty($serve_list)){
	//	$serve_list = array(
	//		array('title'=>'上门保洁')
	//	);
	}
	foreach($serve_list as &$item){
		//已支付，已确认，已完成订单数
		$num1=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'1'));
		$num2=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'2'));
		$num3=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'4'));
		$item['num']=$num1+$num2+$num3;
	}
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	$return['post']=array();
	$return['list']=$serve_list;
	$return['sharedata']=array(
			'title'=>'项目列表',
			'desc'=>$basic['share_desc']?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_gohome/sever//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	set_html('title','项目列表');
	exi($return);
}
if($_RQ['op']=='sever_detail'){
	if(!$_RQ['id']){
		exi('没有找到该项目，请重新选择！','error','jy_gohome/sever/list');
	}
	//查询项目具体信息
	$serve=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$package=$serve['package'];
	//查询本项目可服务员工(未锁定，状态为可服务)
	$worker_list=pdo_getall('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	//查询自定义字段
	$fileds=pdo_getall('jy_gohome_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id']),'','','paixu desc');
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	//查询本项目的访问人数及添加访问人数
	$num=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'num');
	$serve['num']=$num+1;
	pdo_update('jy_gohome_serve',array('num'=>$serve['num']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//已支付，已确认，已完成订单数
	$num1=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'1'));
	$num2=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'2'));
	$num3=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'4'));
	$serve['ordernum']=$num1+$num2+$num3;
	$serve_time=json_decode($serve['serve_time'],true);
	//设置日期
	
	$data=array();
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$days=$basic['days']>0?$basic['days']:7;
	for($i=0;$i<$days;$i++){
		$data[]=array('riqi'=>date("m-d",strtotime("+".$i." day")),'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],'isshow'=>($serve['advancedays']>0&&$i<$serve['advancedays'])?-1:1);
	}
	$weekid=date("w");
	$weekid=$weekid?:7;
	$iswork=strpos($serve['work_time'],';'.$weekid.';');
	
	$this->session('serve_id',$_RQ['id']);
	
	//获取当前时间
	$time=time();
	foreach($serve_time as $k=>&$item){
		$num=pdo_count('jy_gohome_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id']));
		
		if($item['yuyue']>$num){
		$item['yuyue']=$item['yuyue']-$num;
		}
		else{
		$item['yuyue']=0;
		}
		//时间转为时间戳
		$item['shijian']=strtotime(date("Y-m-d",time("day")).$item['times']['start']);
		if($item['shijian']<time() || !$iswork ){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	}
	$return['serve']=$serve;
	$return['package']=json_decode($package);
	$return['worker_list']=$worker_list;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['serve_time']=$serve_time;
	$return['data']=$data;
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));
	$return['post']=array('dayid'=>0,'username'=>$member['nickname'],'mobile'=>$member['mobile']);
	$return['post']['fileds']=$fileds;
	$return['sharedata']=array(
			'title'=>'项目-'.$serve['title'],
			'desc'=>$serve['remark']?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_gohome/sever/detail/id='.$serve['id'].'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$serve['image'],
	);
	set_html('title','项目详情');
	exi($return);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	//日期 今天，未来七天
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
	$serve_id = $this->session('serve_id');
	if($postdata){
		if($postdata['worker_id']==''){
			//exi('请选择服务人员','error');
		}
		//exi($postdata['timeid'],'error');
		if(!$postdata['timeid']){
			exi('请选择服务时间','error');
		}
		if(!$postdata['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$postdata['mobile']){
			exi('请输入联系人电话','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  
	 
			exi('请输入正确的手机号码','error');
		} 
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
			//$package=ijson_encode(array('package_title'=>$postdata['package_title'],'package_price'=>$postdata['package_price']));
			//$time=ijson_encode(array('data'=>$postdata['data'],'start'=>$postdata['start'],'end'=>$postdata['end']));
			$serve=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id));
			$package=$serve['package'];
			$package=json_decode($package,true);
			$merchid=$serve['merchid'];
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'user_id'=>$_SESSION['uid'],
				'merchid'=>	$merchid,
				'worker_id'=>$postdata['worker_id'],
				'serve_id'=>$this->session('serve_id'),
				'package'=>ijson_encode($package[$postdata['packageid']]),
				'timeid'=>$postdata['timeid']-1,
				'day'=>$data[$postdata['dayid']],
				'pcd'=>ijson_encode($postdata['pcd']),
				'address'=>$postdata['address'],
				'username'=>$postdata['username'],
				'mobile'=>$postdata['mobile'],
				'remark'=>$postdata['remark'],
				'jifen'=>$serve['jifen'],
				'ordersn'=>get_ordersn('jy_gohome_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_gohome_order',$insert);
			$id=pdo_insertid();
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				//exi($item['id'],'error');
				$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'user_id'=>$_SESSION['uid'],
				'serve_id'=>$this->session('serve_id'),
				'order_id'=>$id,
				'fileds_id'=>$item['id'],
			//	'data'=>$item['data'],
				'createtime'=>TIMESTAMP,
				);
				if($item['type']==7){
					$data['data']=$item['data']['province'].$item['data']['city'].$item['data']['district'].$item['data']['xiangxi'];
					
				}
				else{
					$data['data']=$item['data'];
				}
				pdo_insert('jy_gohome_data',$data);
			}
		
		}
	}
	exi('','','jy_gohome/book/main/id='.$id);
}
if($_RQ['op']=='serve_time'){
	
	if(!isset($_RQ['id'])){
		exi('请选择项目','error');
	}
	if(!isset($_RQ['day'])){
		exi('请重新选择日期','error');
	}
	$data=array();
	//$data['0']=date("Y-m-d",time("day")); 
	//$data['1']=date("Y-m-d",strtotime("+1 day")); 
	//$data['2']=date("Y-m-d",strtotime("+2 day")); 
	//$data['3']=date("Y-m-d",strtotime("+3 day")); 
	//$data['4']=date("Y-m-d",strtotime("+4 day")); 
	//$data['5']=date("Y-m-d",strtotime("+5 day")); 
	//$data['6']=date("Y-m-d",strtotime("+6 day"));
	$data=$this->days($_RQ['id'],$basic['days']);
	$return['data']=$data;
	$post=get_postdata();
	$work_time=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'work_time');
	$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$_RQ['day']." day"))));
	$weekid=$weekid?:7;
	$iswork=strpos($work_time,';'.$weekid.';');
	$serve_time=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'serve_time');
	$serve_time=json_decode($serve_time,true);
	//print_r($serve_time);
	//exit;
	//获取当前时间
	$time=time();
	if($serve_time){
	foreach($serve_time as $k=>&$item){
		//$num=pdo_count('jy_gohome_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>$data[$_RQ['day']],'serve_id'=>$_RQ['id']));
		//已支付，已确认，已完成订单数
		$num1=pdo_count('jy_gohome_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>$data[$_RQ['day']],'serve_id'=>$_RQ['id'],'status'=>'1'));
		$num2=pdo_count('jy_gohome_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>$data[$_RQ['day']],'serve_id'=>$_RQ['id'],'status'=>'2'));
		$num3=pdo_count('jy_gohome_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>$data[$_RQ['day']],'serve_id'=>$_RQ['id'],'status'=>'4'));
		$num=$num1+$num2+$num3;
		if($item['yuyue']>$num){
		$item['yuyue']=$item['yuyue']-$num;
		}
		else{
		$item['yuyue']=0;
		}
		//时间转为时间戳
		$item['shijian']=strtotime($data[$_RQ['day']]['ymd'].$item['times']['start']);
		if($item['shijian']<time() || $iswork ===false){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	
		
	}
	}
	$return['serve_time']=$serve_time;
	$return['success']=1;
	
	exi($return);
}
if($_RQ['op']=='package'){
	if(!isset($_RQ['id'])){
		exi('请重新选择项目','error');
	}
	//exi($_RQ['id'],'error');
	$package=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	//查询自定义字段
	$fileds=pdo_getall('jy_gohome_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}
